package com.qf.controller;

import com.qf.entity.Admin;
import com.qf.entity.Product;
import com.qf.entity.Shop;
import com.qf.entity.User;
import com.qf.entity.custom.AdminCustom;
import com.qf.entity.custom.ProductCustom;
import com.qf.entity.custom.ShopCustom;
import com.qf.entity.custom.UserCustom;
import com.qf.service.AdminService;
import com.qf.service.ShopService;
import com.qf.tools.JavaWebToken;
import com.qf.tools.ResponseUtils;
import com.qf.tools.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author zhangyubin
 * @Date 2020/7/26 18:17
 */
@RestController
@RequestMapping("/shopManage")
public class ShopManageController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private AdminService adminService;


    /**
     * 店铺管理登录接口：以实体类对象形式接收前端传输的数据，解析得到shopLogin和shopPwd
     * 判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     * 操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param shop shop实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultEntity login(@RequestBody Shop shop) {
        String username = shop.getShopLogin();
        String password = shop.getShopPwd();
        if (username != null && username != "" && password != null && password != "") {
            Shop shop1 = shopService.login(username, password);
            Admin admin = adminService.login(username, password);
            if (shop1 != null) {
                Map<String, Object> m = new HashMap<String, Object>();
                m.put("shopId", shop1.getShopId());
                String javaWebToken = JavaWebToken.createJavaWebToken(m);
                ShopCustom shopCustom = new ShopCustom();
                shopCustom.setToken(javaWebToken);
                shopCustom.setID("商家");
                return ResultEntity.success(shopCustom);
            }if (admin != null){
                Map<String, Object> m = new HashMap<String, Object>();
                m.put("adminId", admin.getAdminId());
                String javaWebToken = JavaWebToken.createJavaWebToken(m);
                AdminCustom adminCustom = new AdminCustom();
                adminCustom.setToken(javaWebToken);
                adminCustom.setID("管理员");
                return ResultEntity.success(adminCustom);
            }
            else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }


    /**
     * 注册接口：以实体类对象形式接收前端传输的数据，解析得到username和password
     *           判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     *           操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param shop shop实体类接收json字符串
     *
     * @return 返回ResultEntity类型的成功或者失败信息
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResultEntity register(@RequestBody ShopCustom shop) {
        String shopLogin = shop.getShopLogin();
        String shopPwd = shop.getShopPwd();
        if (shopLogin != null && shopLogin != "" && shopPwd != null && shopPwd != "") {
            return ResponseUtils.responseClinet(shopService.register(shop));
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }


    /**
     * 店铺商品显示接口：以实体类对象形式接收前端传输的数据，解析得到shopLogin和shopPwd
     * 判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     * 操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param shop shop实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/getShopProductByShopId", method = RequestMethod.POST)
    public ResultEntity getShopProductByShopId(@RequestBody ShopCustom shop) {
        String token = shop.getToken();
        if (token != null) {
            Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer shopId = (Integer) map.get("shopId");
            List<ShopCustom> shopProductByShopId = shopService.getShopProductByShopId(shopId);
            if (shopProductByShopId != null) {
                return ResultEntity.success(shopProductByShopId);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }

    /**
     * 修改商品接口：以实体类对象形式接收前端传输的数据，解析得到shopLogin和shopPwd
     * 判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     * 操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param product product实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/updateShopProductById", method = RequestMethod.POST)
    public ResultEntity updateShopProductById(@RequestBody ProductCustom product) {

        Integer productId = product.getProductId();
        String token = product.getToken();
        if (token != null) {
            Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer shopId = (Integer) map.get("shopId");
            if (productId != null) {
                boolean b = shopService.modifyShopProductByShopId(product);
                if (b == true) {
                    List<ShopCustom> shopProductByShopId = shopService.getShopProductByShopId(shopId);
                    return ResultEntity.success(shopProductByShopId);
                } else {
                    return ResponseUtils.responseClinet(false);
                }
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }


    /**
     * 删除商品接口：以实体类对象形式接收前端传输的数据，解析得到shopLogin和shopPwd
     * 判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     * 操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param product product实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/deleteShopProductById", method = RequestMethod.POST)
    public ResultEntity deleteShopProductById(@RequestBody ProductCustom product) {

        Integer productId = product.getProductId();
        String token = product.getToken();
        if (token != null) {
            Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer shopId = (Integer) map.get("shopId");
            if (productId != null) {
                boolean b = shopService.deleteShopProductByShopId(product);
                if (b == true) {
                    List<ShopCustom> shopProductByShopId = shopService.getShopProductByShopId(shopId);
                    return ResultEntity.success(shopProductByShopId);
                } else {
                    return ResponseUtils.responseClinet(false);
                }
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }


    /**
     * 根据商品标题显示接口：以实体类对象形式接收前端传输的数据，解析得到shopLogin和shopPwd
     * 判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     * 操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param shop shop实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/getShopProductByTitle", method = RequestMethod.POST)
    public ResultEntity getShopProductByTitle(@RequestBody ShopCustom shop) {
        String token = shop.getToken();
        String productTitle = shop.getProductTitle();
        if (token != null && productTitle != null) {
            Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer shopId = (Integer) map.get("shopId");
            List<ShopCustom> shopProductByShopId = shopService.getShopProductByTitle(shopId,productTitle);
            if (shopProductByShopId != null) {
                return ResultEntity.success(shopProductByShopId);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }


    /**
     * 根据商品类型显示接口：以实体类对象形式接收前端传输的数据，解析得到shopLogin和shopPwd
     * 判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     * 操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param shop shop实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/getShopProductByType", method = RequestMethod.POST)
    public ResultEntity getShopProductByType(@RequestBody ShopCustom shop) {
        String token = shop.getToken();
        String productType = shop.getProductType();
        if (token != null && productType != null) {
            Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer shopId = (Integer) map.get("shopId");
            List<ShopCustom> shopProductByShopId = shopService.getShopProductByType(shopId,productType);
            if (shopProductByShopId != null) {
                return ResultEntity.success(shopProductByShopId);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }


    /**
     * 店铺信息接口：以实体类对象形式接收前端传输的数据，解析得到shopLogin和shopPwd
     * 判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     * 操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param shop shop实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/getShopById", method = RequestMethod.POST)
    public ResultEntity getShopById(@RequestBody ShopCustom shop) {
        String token = shop.getToken();
        if (token != null) {
            Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer shopId = (Integer) map.get("shopId");
            Shop shopById = shopService.getShopById(shopId);
            if (shopById != null) {
                return ResultEntity.success(shopById);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }


    /**
     * 店铺修改信息接口：以实体类对象形式接收前端传输的数据，解析得到shopLogin和shopPwd
     * 判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     * 操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param shop shop实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/modifyShopById", method = RequestMethod.POST)
    public ResultEntity modifyShopById(@RequestBody ShopCustom shop) {
        String token = shop.getToken();
        if (token != null) {
            Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer shopId = (Integer) map.get("shopId");
            shop.setShopId(shopId);
            boolean b = shopService.modifyShopById(shop);
            if (b == true) {
                Shop shopById = shopService.getShopById(shopId);
                return ResultEntity.success(shopById);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }

    /**
     * 店铺顾客数量接口：以实体类对象形式接收前端传输的数据，解析得到shopLogin和shopPwd
     * 判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     * 操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param shop shop实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/getUserCount", method = RequestMethod.POST)
    public ResultEntity getUserCount(@RequestBody ShopCustom shop) {
        String token = shop.getToken();
        if (token != null) {
            Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer shopId = (Integer) map.get("shopId");
            int userShopCount = shopService.getUserShopCount(shopId);

            if (userShopCount != 0) {
                return ResultEntity.success(userShopCount);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }

    /**
     * 店铺新增顾客接口：以实体类对象形式接收前端传输的数据，解析得到shopLogin和shopPwd
     * 判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     * 操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param shop shop实体类接收json字符串
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/getUserShopCountIsNew", method = RequestMethod.POST)
    public ResultEntity getUserShopCountIsNew(@RequestBody ShopCustom shop) throws ParseException {
        String token = shop.getToken();
        if (token != null) {
            Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
            Integer shopId = (Integer) map.get("shopId");
            Date date = new Date();//获得系统时间.
            SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd " );
            String nowTime = sdf.format(date);
            Date time = sdf.parse( nowTime );
            int userShopCountIsNew = shopService.getUserShopCountIsNew(shopId, time);
            if (userShopCountIsNew != 0) {
                return ResultEntity.success(userShopCountIsNew);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }

}
